/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.five9.usertools;

/**
 *
 * @author nromero
 */
import com.five9.api74.AccessDisallowedFault;
import com.five9.api74.AdminSessionClosedFault;
import com.five9.api74.ConcurrentModificationFault;
import com.five9.api74.IncorrectArgumentFault;
import com.five9.api74.InvalidAccountFault;
import com.five9.api74.InvalidRegexpPatternFault;
import com.five9.api74.MissedArgumentFault;
import com.five9.api74.OperationsLimitExceededFault;
import com.five9.api74.ServerFault;
import com.five9.api74.ServiceUnavailableFault;
import com.five9.api74.SkillNotFoundFault;
import com.five9.api74.UserAlreadyLoggedInFault;
import com.five9.api74.UserDoesntHaveSkillFault;
import com.five9.api74.UserInfo;
import com.five9.api74.UserNotFoundFault;
import com.five9.api74.UserSkill;
import com.five9.util.ConsoleLoggingHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.ws.BindingProvider;

/**
 *
 * @author nromero
 */
public class UserEdit {

    static final Logger logger = Logger.getLogger(UserEdit.class.getName());
    static String username = "_five9@BrownGreer";
    static String password = "five99!";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        logger.setLevel(Level.CONFIG);
        java.util.logging.Handler handler = ConsoleLoggingHandler.getInstance();
        handler.setLevel(Level.OFF);
        logger.addHandler(handler);
        logger.log(Level.INFO, "UserTools");

        for (String s : args) {
            logger.log(Level.CONFIG, s);
            if (s.equalsIgnoreCase("-cu")) {
                //
            }
        }
        try {
            List<UserInfo> usersInfo = getUsersInfo(".*", username, password);
            List<UserInfo> usersInfoToMofify = new ArrayList();
            UserInfo tui = null;
            for (UserInfo ui : usersInfo) {
                logger.log(Level.INFO, "user: {0} | \"{1}\"", new Object[]{ui.getGeneralInfo().getUserName(), ui.getGeneralInfo().getPassword()});
//                if (ui.getRoles().getSupervisor() == null && ui.getRoles().getAdmin() == null && ui.getGeneralInfo().getExtension() >= 1000){
//                        usersInfoToMofify.add(ui);
//                }
                UserSkill us = new UserSkill();
                us.setLevel(1);
                us.setSkillName("Supervisors - ALL CCS");
                us.setUserName(ui.getGeneralInfo().getUserName());
                if (ui.getRoles().getSupervisor() != null && ui.getSkills().contains(us)) {
                    usersInfoToMofify.add(ui);
                }
//                if (ui.getGeneralInfo().getUserName().equalsIgnoreCase("0001 Agent@browngreer")) {
//                    usersInfoToMofify.add(ui);
//                    tui = ui;
//                }
            }
            logger.log(Level.INFO, "users to modify: {0}", usersInfoToMofify.size());
            for (UserInfo ui : usersInfoToMofify) {
                try {
                    logger.log(Level.INFO, "user: {0} being modified", tui.getGeneralInfo().getUserName());
                    ui.getGeneralInfo().setCanChangePassword(Boolean.FALSE);
                    //modify the can change password flag
//                    modifyUser(ui.getGeneralInfo(), ui.getRoles(), new ArrayList<com.five9.api74.UserRoleType>(), username, password);

                    // change skill membership 
                    List<UserSkill> uskills = new ArrayList();
                    List<String> sknames = new ArrayList();
                    sknames.add("Supervisors - RDU Operators");
                    for (String sn : sknames) {
                        UserSkill us = new UserSkill();
                        us.setSkillName(sn);
                        us.setLevel(1);
                        us.setUserName(ui.getGeneralInfo().getUserName());
                    }
                    ui.getSkills();
                    modifyUser(ui.getGeneralInfo(), ui.getRoles(), new ArrayList<com.five9.api74.UserRoleType>(), username, password);
                    logger.log(Level.INFO, "user: {0} updated", tui.getGeneralInfo().getUserName());
                } catch (UserNotFoundFault ex) {
                    Logger.getLogger(UserEdit.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ConcurrentModificationFault ex) {
                    Logger.getLogger(UserEdit.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (AdminSessionClosedFault ex) {
            Logger.getLogger(UserEdit.class.getName()).log(Level.SEVERE, null, ex);
        } catch (OperationsLimitExceededFault ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (InvalidRegexpPatternFault ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (InvalidAccountFault ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (IncorrectArgumentFault ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (UserAlreadyLoggedInFault ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (ServiceUnavailableFault ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (AccessDisallowedFault ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (MissedArgumentFault ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (ServerFault ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }

    private static java.util.List<com.five9.api74.UserInfo> getUsersInfo(java.lang.String userNamePattern, String username, String password) throws AdminSessionClosedFault, OperationsLimitExceededFault, InvalidRegexpPatternFault, InvalidAccountFault, IncorrectArgumentFault, UserAlreadyLoggedInFault, ServiceUnavailableFault, AccessDisallowedFault, MissedArgumentFault, ServerFault {
        com.five9.api74.WsAdminService service = new com.five9.api74.WsAdminService();
        com.five9.api74.WsAdmin port = service.getWsAdminPort();
        ((BindingProvider) port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://api.five9.com/wsadmin/AdminWebService");
        ((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
        ((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
        return port.getUsersInfo(userNamePattern);
    }

    private static UserInfo modifyUser(com.five9.api74.UserGeneralInfo userGeneralInfo, com.five9.api74.UserRoles rolesToSet, java.util.List<com.five9.api74.UserRoleType> rolesToRemove, String username, String password) throws InvalidAccountFault, AdminSessionClosedFault, IncorrectArgumentFault, UserNotFoundFault, OperationsLimitExceededFault, ServiceUnavailableFault, MissedArgumentFault, UserAlreadyLoggedInFault, AccessDisallowedFault, ConcurrentModificationFault, ServerFault {
        com.five9.api74.WsAdminService service = new com.five9.api74.WsAdminService();
        com.five9.api74.WsAdmin port = service.getWsAdminPort();

        ((BindingProvider) port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://api.five9.com/wsadmin/AdminWebService");
        ((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
        ((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
        return port.modifyUser(userGeneralInfo, rolesToSet, rolesToRemove);
    }

    private static void userSkillRemove(com.five9.api74.UserSkill userSkill, String username, String password) throws InvalidAccountFault, ConcurrentModificationFault, ServiceUnavailableFault, AdminSessionClosedFault, UserAlreadyLoggedInFault, UserNotFoundFault, ServerFault, IncorrectArgumentFault, MissedArgumentFault, SkillNotFoundFault, OperationsLimitExceededFault, UserDoesntHaveSkillFault, AccessDisallowedFault {
        com.five9.api74.WsAdminService service = new com.five9.api74.WsAdminService();
        com.five9.api74.WsAdmin port = service.getWsAdminPort();
        ((BindingProvider) port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://api.five9.com/wsadmin/AdminWebService");
        ((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
        ((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
        port.userSkillRemove(userSkill);
    }
}
